<?php
class ControllerAccountEdit extends Controller {
	private $error = array();

	public function index() {
		if (!$this->customer->isLogged()) {
			$this->session->data['redirect'] = $this->url->link('account/edit', '', 'SSL');

			$this->redirect(HTTP_SERVER . 'dang-nhap');
		}

		$this->language->load('account/edit');
		
		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('account/customer');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_account_customer->editCustomer($this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect(HTTP_SERVER . 'thong-tin-tai-khoan');
		}

      	$this->data['breadcrumbs'] = array();

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_home'),
			'href'      => HTTP_SERVER,     	
        	'separator' => false
      	); 

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_account'),
			'href'      => HTTP_SERVER . 'thong-tin-tai-khoan',        	
        	'separator' => $this->language->get('text_separator')
      	);

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_edit'),
			'href'      => $this->url->link('account/edit', '', 'SSL'),       	
        	'separator' => $this->language->get('text_separator')
      	);
		
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_your_details'] = $this->language->get('text_your_details');

		$this->data['entry_firstname'] = $this->language->get('entry_firstname');
		$this->data['entry_lastname'] = $this->language->get('entry_lastname');
		$this->data['entry_email'] = $this->language->get('entry_email');
		$this->data['entry_telephone'] = $this->language->get('entry_telephone');

		$this->data['button_continue'] = $this->language->get('button_continue');
		$this->data['button_back'] = $this->language->get('button_back');

		$this->data['check_image'] = $this->url->link('account/edit/checkimage');

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->error['firstname'])) {
			$this->data['error_firstname'] = $this->error['firstname'];
		} else {
			$this->data['error_firstname'] = '';
		}

		if (isset($this->error['lastname'])) {
			$this->data['error_lastname'] = $this->error['lastname'];
		} else {
			$this->data['error_lastname'] = '';
		}
		
		if (isset($this->error['email'])) {
			$this->data['error_email'] = $this->error['email'];
		} else {
			$this->data['error_email'] = '';
		}	

		$this->data['action'] = $this->url->link('account/edit', '', 'SSL');

		if ($this->request->server['REQUEST_METHOD'] != 'POST') {
			$customer_info = $this->model_account_customer->getCustomer($this->customer->getId());
		}

		if (isset($this->request->post['firstname'])) {
			$this->data['firstname'] = $this->request->post['firstname'];
		} elseif (isset($customer_info)) {
			$this->data['firstname'] = $customer_info['firstname'];
		} else {
			$this->data['firstname'] = '';
		}

		if (isset($this->request->post['lastname'])) {
			$this->data['lastname'] = $this->request->post['lastname'];
		} elseif (isset($customer_info)) {
			$this->data['lastname'] = $customer_info['lastname'];
		} else {
			$this->data['lastname'] = '';
		}

		if (isset($this->request->post['email'])) {
			$this->data['email'] = $this->request->post['email'];
		} elseif (isset($customer_info)) {
			$this->data['email'] = $customer_info['email'];
		} else {
			$this->data['email'] = '';
		}

		if (isset($customer_info['thumb_url']) && $customer_info['thumb_url'] != '' && file_exists(DIR_IMAGE . $customer_info['thumb_url'])) {
			$this->data['thumb_url'] = 'image/' . $customer_info['thumb_url'];
		}else {
			$this->data['thumb_url'] = 'image/no_image.jpg';
		}

		$this->data['back'] = HTTP_SERVER . 'thong-tin-tai-khoan';

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/edit.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/account/edit.tpl';
		} else {
			$this->template = 'default/template/account/edit.tpl';
		}
		
		$this->children = array(
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'	
		);
						
		$this->response->setOutput($this->render());	
	}

	private function validate() {
		if ((utf8_strlen($this->request->post['firstname']) < 1) || (utf8_strlen($this->request->post['firstname']) > 32)) {
			$this->error['firstname'] = $this->language->get('error_firstname');
		}

		if ((utf8_strlen($this->request->post['lastname']) < 1) || (utf8_strlen($this->request->post['lastname']) > 32)) {
			$this->error['lastname'] = $this->language->get('error_lastname');
		}

		if ((utf8_strlen($this->request->post['email']) > 96) || !preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email'])) {
			$this->error['email'] = $this->language->get('error_email');
		}
		
		if (($this->customer->getEmail() != $this->request->post['email']) && $this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
			$this->error['warning'] = $this->language->get('error_exists');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	public function checkimage() {
		$json = array();
		if (isset($this->request->post) && $_SERVER['REQUEST_METHOD'] == 'POST' && $this->customer->isLogged()) {
			$file_name = $_FILES['user_thumb']['name'];
			$file_size = $_FILES['user_thumb']['size'];
			$valid_format = array("jpg", "png", "gif", "bmp");
			if (strlen($file_name)) {
				list($txt, $ext) = explode(".", $file_name);
				if (in_array($ext, $valid_format)) {
					if ($file_size < (1024*1024)) {
						$path = DIR_IMAGE . 'data/user/';
						$name = 'user_thumb' . $this->customer->getId() . '.' . $ext;
						if (file_exists($path . $name)) {
							unlink($path . $name);
						}
						$tmp = $_FILES['user_thumb']['tmp_name'];
						if (move_uploaded_file($tmp, $path . $name)) {
							$this->load->model('account/customer');
							$this->model_account_customer->setThumb('data/user/'. $name, $this->customer->getId());
							$json['status'] = 'success';
							$json['image'] = 'image/data/user/'. $name;
							$json['message'] = 'User thumb change success !';
						}else {
							$json['status'] = 'failed';
							$json['message'] = 'Upload file failed !';
						}
					}else {
						$json['status'] = 'failed';
						$json['message'] = 'Image file size max 1MB';
					}
				}else {
					$json['status'] = 'failed';
					$json['message'] = 'Invalid file format..';
				}
			}else {
				$json['status'] = 'failed';
				$json['message'] = 'Please select a Image..!';
			}
		}

		$this->response->setOutput(json_encode($json));
	}
}
?>